package com.hbuzzs.elasticsearch.service.impl;

import com.google.common.collect.Lists;
import com.hbuzzs.elasticsearch.dao.TestBeanDaoRepository;
import com.hbuzzs.elasticsearch.entity.TestBean;
import com.hbuzzs.elasticsearch.service.TestService;
import com.hbuzzs.elasticsearch.util.ElasticsearchUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 添加数据是模拟的
 *
 * @author zhangzhishun
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TestServiceImpl implements TestService {

    private final TestBeanDaoRepository testBeanDaoRepository;

    @Override
    public Boolean save(TestBean bean) {
        TestBean t = testBeanDaoRepository.save(bean);
        log.info("插入数据返回值：{}", t);
        return true;
    }

    @Override
    public void delete(TestBean bean) {
        log.info("删除数据：{}", bean);
        testBeanDaoRepository.delete(bean);
    }

    @Override
    public Boolean update(TestBean bean) {
        TestBean t = testBeanDaoRepository.save(bean);
        log.info("更新数据返回值：{}", t);
        return true;
    }

    @Override
    public List<TestBean> findAll() {
        log.info("查看es所有数据");
        return Lists.newArrayList(testBeanDaoRepository.findAll());
    }

    @Override
    public List<TestBean> findByName(String name) {
        log.info("精确查找：{}", name);
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        List<QueryBuilder> queryBuilders = queryBuilder.filter();
        queryBuilders.add(QueryBuilders.termQuery("name", name));
        queryBuilders.add(QueryBuilders.boolQuery().should(QueryBuilders.termQuery("name", name)));
        return Lists.newArrayList(testBeanDaoRepository.search(queryBuilder));
    }

    @Override
    public List<TestBean> findByNameFuzzyQuery(String name) {
        log.info("模糊查找：{}", name);
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        List<QueryBuilder> must = queryBuilder.filter();
        must.add(QueryBuilders.wildcardQuery("name", "*" + name + "*"));
        ElasticsearchUtil.printParam(queryBuilder);
        return Lists.newArrayList(testBeanDaoRepository.search(queryBuilder));
    }


    @Override
    public List<TestBean> findManByNameOrDesc(String name, String desc) {
        log.info("and和or连接查找：男生，姓名为{}或者描述中包含：{}", name, desc);
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        List<QueryBuilder> queryBuilders = queryBuilder.filter();
        queryBuilders.add(QueryBuilders.termQuery("sex", "男"));
        queryBuilders.add(QueryBuilders.boolQuery()
                .should(QueryBuilders.termQuery("name", name))
                .should(QueryBuilders.wildcardQuery("desc", "*" + desc + "*"))
        );
        return Lists.newArrayList(testBeanDaoRepository.search(queryBuilder));
    }

    @Override
    public List<TestBean> findFirstPage(Integer startPage, Integer n) {
        log.info("查看第{}页，每页数量：{}", startPage, n);
        return Lists.newArrayList(testBeanDaoRepository.findAll(PageRequest.of(startPage, n)));
    }
}

